/*
 * Copyright (C) 2017 zhouyou(478319399@qq.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.shuyihui.rxhttp.interceptor

import com.shuyihui.rxhttp.utils.HttpLog
import com.shuyihui.rxhttp.utils.HttpUtil
import okhttp3.*
import java.io.IOException

/**
 *
 * 描述：判断响应是否有效的处理
 * 继承后扩展各种无效响应处理：包括token过期、账号异地登录、时间戳过期、签名sign错误等<br></br>
 */
abstract class BaseExpiredInterceptor : Interceptor {
    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {
        val request: Request = chain.request()
        val response: Response = chain.proceed(request)
        val responseBody = response.body
        val source = responseBody!!.source()
        source.request(Long.MAX_VALUE) // Buffer the entire body.
        val buffer = source.buffer()
        var charset = HttpUtil.UTF8
        val contentType = responseBody.contentType()
        if (contentType != null) {
            charset = contentType.charset(HttpUtil.UTF8)
        }
        val bodyString = buffer.clone().readString(charset!!)
        HttpLog.i("网络拦截器:" + bodyString + " host:" + request.url.toString())
        val isText = isText(contentType)
        if (!isText) {
            return response
        }
        //判断响应是否过期（无效）
        return if (isResponseExpired(response, bodyString)) {
            responseExpired(chain, bodyString)
        } else response
    }

    private fun isText(mediaType: MediaType?): Boolean {
        if (mediaType == null) return false
        if (mediaType.type != null && mediaType.type == "text") {
            return true
        }
        if (mediaType.subtype != null) {
            if (mediaType.subtype == "json") {
                return true
            }
        }
        return false
    }

    /**
     * 处理响应是否有效
     */
    abstract fun isResponseExpired(response: Response?, bodyString: String?): Boolean

    /**
     * 无效响应处理
     */
    abstract fun responseExpired(chain: Interceptor.Chain?, bodyString: String?): Response
}